<!DOCTYPE html>

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>正则表达式</title>
</head>

<body>
    <input type="text" id="old"><button onclick="abc()">过滤空格</button></<br>
    <input type="text" id="new">
    <script>
       function abc(){
           var oldstr = document.getElementById('old');
           var newstr = document.getElementById('new');
           var re = /^\s*|\s*$/g
           newstr.value=oldstr.value.replace(re,'') 
       }
    </script>
</body>

</html>